package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
 *@Description:1
 *@1author:刘哥
 *@Date:15:18 2022/7/14
 */
@Controller
public class DownLoadController {
    @RequestMapping("/downLoad")
    public void downLoad(String name, HttpSession session, HttpServletResponse response) throws IOException {
        System.out.println("name = " + name);
//        获取上传得我路径
        String baselPath = session.getServletContext().getRealPath("upload");
        String filePath=baselPath+ File.separator+name;//upload/101.jpg
//        设置响应头，告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名 ***
        response.setHeader("content-disposition","attachment;filename="+name);
        IOUtils.copy(new FileInputStream(filePath),response.getOutputStream());
    }
}
